package com.xcw.bos.service.impl;


import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xcw.bos.dao.IRoleDao;
import com.xcw.bos.domain.Function;
import com.xcw.bos.domain.Role;
import com.xcw.bos.service.IRoleService;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleDao roleDao;
    @Autowired
    private ProcessEngine processEngine;
    
    //保存角色
	public void save(Role role, String functionIds) {
		roleDao.save(role);
		Group group = new GroupEntity();
		group.setId(role.getName());////角色名称对应组id
		group.setName(role.getName());
		processEngine.getIdentityService().saveGroup(group);
		if(StringUtils.isNotBlank(functionIds)){
			String[] ids = functionIds.split(",");
			for(String functionId : ids){
				//构造脱管对象
				Function function = new Function();
				function.setId(functionId);
				role.getFunctions().add(function);//角色关联权限
			}
		}

	}

	
	public List<Role> findAll() {
		
		return roleDao.findAll();
	}

}
